package cn.t.base;

import cn.t.core.mode.BaseDO;
import cn.t.core.mode.BasePO;
import cn.t.core.mode.YesNoEnum;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import org.springframework.util.ObjectUtils;

import java.util.Date;

/**
 * @author l
 */

public class BaseWrapper {
    public static <T extends BasePO,D extends BaseDO> void updateWrapper(D baseDO, LambdaUpdateChainWrapper<T> wrapper) {
        if (!ObjectUtils.isEmpty(baseDO.getGmtModified())) {
            wrapper.set(T::getGmtModified, baseDO.getGmtModified());
        } else {
            wrapper.set(T::getGmtModified, new Date());
        }
        if (!ObjectUtils.isEmpty(baseDO.getModifier())) {
            wrapper.set(T::getModifier, baseDO.getModifier());
        }
        isDeleteWrapper(baseDO, wrapper);
    }

    public static <T extends BasePO> void getCreateWrapper(T baseDO, LambdaUpdateChainWrapper<T> wrapper) {
        wrapper.set(T::getCreator, baseDO.getCreator());
        wrapper.set(T::getGmtCreated, new Date());
        wrapper.set(T::getIsDeleted, YesNoEnum.FALSE);

    }
    public static <T extends BasePO> void getCreatePo(T baseDO) {
        baseDO.setGmtCreated(new Date());
        baseDO.setIsDeleted(YesNoEnum.FALSE);

    }
    public static <T extends BasePO> void unDeleteWrapper(LambdaQueryChainWrapper<T> wrapper) {
        wrapper.eq(T::getIsDeleted, YesNoEnum.FALSE);
    }

    public static  <T extends BasePO,D extends BaseDO> void isDeleteWrapper(D baseDO, LambdaUpdateChainWrapper<T> wrapper) {
        if (!ObjectUtils.isEmpty(baseDO.getIsDeleted())) {
            wrapper.set(T::getIsDeleted, baseDO.getIsDeleted());
        }
    }
}
